 //
//  GameViewController.swift
//  DouYuZB
//
//  Created by James on 2017/10/25.
//  Copyright © 2017年 James. All rights reserved.
//

import UIKit

private let kEdgeMargin:CGFloat = 10
private let kItemW:CGFloat = (kScreenW - 2*kEdgeMargin)/3
private let kItemH:CGFloat = kItemW * 6 / 5;
private let kCellId = "GameViewController"
private let kHeaderId = "kHeaderId"
 
private let kHeaderViewH:CGFloat = 50
 
class GameViewController: UIViewController {
    
    lazy var gameVM = GameViewModel()
    
    
    fileprivate lazy var collectionView:UICollectionView = {[weak self] in
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: kItemW, height: kItemH)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.sectionInset = UIEdgeInsetsMake(0, kEdgeMargin, 0, kEdgeMargin)
        layout.headerReferenceSize = CGSize(width: kScreenW, height: kHeaderViewH)
        
        
        let collectionView = UICollectionView(frame: self!.view.bounds, collectionViewLayout: layout)
        
        collectionView.register(UINib(nibName: "CollectionGameCell", bundle: nil), forCellWithReuseIdentifier: kCellId)
        
        collectionView.register(UINib(nibName: "CollectionHeaderView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kHeaderId)
        
        collectionView.backgroundColor = UIColor.white
        collectionView.dataSource = self
        collectionView.delegate = self
        
        if #available(iOS 11,*){
            collectionView.contentInsetAdjustmentBehavior = .never
        }
        
        return collectionView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        view.backgroundColor = UIColor.white
        view.addSubview(collectionView)
        print(view.frame)
        collectionView.frame = view.bounds
        print(collectionView.frame)
        loadData()
    }
}

 extension GameViewController{
    private func loadData() {
        gameVM.loadAllGameData {[weak self] in
            self?.collectionView.reloadData()
        }
    }
 }
 
extension GameViewController : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let gameCell = collectionView.dequeueReusableCell(withReuseIdentifier: kCellId, for: indexPath) as! CollectionGameCell
        
        let gameModel = gameVM.dataModels[indexPath.item]
        gameCell.group = gameModel
        
        return gameCell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind:kind , withReuseIdentifier: kHeaderId, for: indexPath) as! CollectionHeaderView
        
        headerView.titleLabel.text = "全部"
        headerView.moreButton.isHidden = true
        
        headerView.iconImageView.image = UIImage(named:"Img_orange")
        return headerView
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return gameVM.dataModels.count
    }
 }
 
